<?php
class Admin_OverallController extends Ctrl_Admin{
    public function indexAction(){
        $p = $_REQUEST;

        $tOMO = new OverallModel; 
        if($_SERVER['REQUEST_METHOD'] == 'POST'){
            $pDescription = empty($p['description'])?Tool_Fnc::ajaxMsg('员工规则不能为空'):Tool_Fnc::safe_string($p['description']);
            $pZsstarttime = empty($p['zs_starttime'])?Tool_Fnc::ajaxMsg('早上开始时间不能为空'):Tool_Fnc::safe_string($p['zs_starttime']);
            $pZsendtime = empty($p['zs_endtime'])?Tool_Fnc::ajaxMsg('早上结束时间不能为空'):Tool_Fnc::safe_string($p['zs_endtime']);
            $pSwstarttime = empty($p['sw_starttime'])?Tool_Fnc::ajaxMsg('上午开始时间不能为空'):Tool_Fnc::safe_string($p['sw_starttime']);
            $pSwendtime = empty($p['sw_endtime'])?Tool_Fnc::ajaxMsg('上午结束时间不能为空'):Tool_Fnc::safe_string($p['sw_endtime']);
            $pZwstarttime = empty($p['zw_starttime'])?Tool_Fnc::ajaxMsg('中午开始时间不能为空'):Tool_Fnc::safe_string($p['zw_starttime']);
            $pZwendtime = empty($p['zs_endtime'])?Tool_Fnc::ajaxMsg('中午结束时间不能为空'):Tool_Fnc::safe_string($p['zw_endtime']);
            $pXwstarttime = empty($p['xw_starttime'])?Tool_Fnc::ajaxMsg('下午开始时间不能为空'):Tool_Fnc::safe_string($p['xw_starttime']);
            $pXwendtime = empty($p['xw_endtime'])?Tool_Fnc::ajaxMsg('下午结束时间不能为空'):Tool_Fnc::safe_string($p['xw_endtime']);
            $pWsstarttime = empty($p['ws_starttime'])?Tool_Fnc::ajaxMsg('晚上开始时间不能为空'):Tool_Fnc::safe_string($p['ws_starttime']);
            $pWsendtime = empty($p['ws_endtime'])?Tool_Fnc::ajaxMsg('晚上结束时间不能为空'):Tool_Fnc::safe_string($p['ws_endtime']);
            $tTime = time();

            $tData = array(
                'description' => $pDescription,
                'zs_starttime' => $pZsstarttime,
                'zs_endtime' => $pZsendtime,
                'sw_starttime' => $pSwstarttime,
                'sw_endtime' => $pSwendtime,
                'zw_starttime' => $pZwstarttime,
                'zw_endtime' => $pZwendtime,
                'xw_starttime' => $pXwstarttime,
                'xw_endtime' => $pXwendtime,
                'ws_starttime' => $pWsstarttime,
                'ws_endtime' => $pWsendtime,
                'id' => 1,
                'updated' => $tTime,
            );
            if(!$tOMO->update($tData)){
                Tool_Fnc::ajaxMsg('修改失败');
            }
            Tool_Fnc::ajaxMsg('修改成功',1);
        }
        $tEIMO = new EventinfoModel;
        $tEIDatas = $tEIMO->field('num,introduce')->fList();


        $tOData = $tOMO->field('*')->fRow();

        $tEMMO = new EventmediaModel;
        $tImgUrl = Yaf_Registry::get("config")->web->url->img;

        $tWhere = ' 1 and em.status = 0';

        $tSql = 'select count(*) c from '.$tEMMO->table.' em left join '.$tEIMO->table.' ei on em.event_type = ei.num where '.$tWhere;
        $tCount = $tEMMO->query($tSql);
        $tCnt = empty($tCount[0]['c'])?0:$tCount[0]['c'];
        $tLimit = 10;
        $tPage = new Tool_Page($tCnt,$tLimit);
        if(!empty($tCnt)){
            $tSql = 'select em.id id,ei.introduce event_type,ei.num event_num,concat(\''.$tImgUrl.'\',em.audio) audio,concat(\''.$tImgUrl.'\',em.video) video,em.audio_title audio_title,em.video_title video_title,em.remarks remarks from '.$tEMMO->table.' em left join '.$tEIMO->table.' ei on em.event_type = ei.num where '.$tWhere.' order by em.id desc limit '.$tPage->limit();
            $tEMDatas = $tEMMO->query($tSql);
        }
        $this->assign('tEIDatas',$tEIDatas);
        $this->assign('tEMDatas',$tEMDatas);
        $this->assign('tShow',$tPage->show());
        $this->assign('tOData',$tOData);
    
    }
    public function editmediaAction(){
        $p = $_REQUEST;
        $pEventtype = empty($p['event_type'])?Tool_Fnc::ajaxMsg('事件类型不能为空'):Tool_Fnc::safe_string($p['event_type']);
        $pTitle = empty($p['title'])?'':Tool_Fnc::safe_string($p['title']);
        $pRemarks = empty($p['remarks'])?'':Tool_Fnc::safe_string($p['remarks']);

        $tEMMO = new EventmediaModel;
        $tEData = $tEMMO->field('id')->where('event_type = '.$pEventtype)->fRow();
        $tTime = time();

        if(!empty($tEData['id'])){
            $tData = array(
                'id' => $tEData['id'],
                'title' => $pTitle,
                'remarks' => $pRemarks,
                'updated' => $tTime,    
            );
            if(!$tEMMO->update($tData)){
                Tool_Fnc::ajaxMsg('添加失败');
            }
            Tool_Fnc::ajaxMsg('添加成功',1);
        }
        $tData = array(
            'title' => $pTitle,
            'remarks' => $pRemarks,
            'event_type' => $pEventtype,
            'created' => $tTime,
        );
        if(!$tEMMO->insert($tData)){
            Tool_Fnc::ajaxMsg('添加失败');
        }
        Tool_Fnc::ajaxMsg('添加成功',1);
    }
    public function deleteAction(){
        $p = $_REQUEST;
        $pId = empty($p['id'])?Tool_Fnc::ajaxMsg('ID错误'):Tool_Fnc::safe_string($p['id']);

        $tEMMO = new EventmediaModel;
        $tData = array(
            'id' => $pId,
            'status' => 1,
            'updated' => time(),
        );
        if(!$tEMMO->update($tData)){
            Tool_Fnc::ajaxMsg('删除失败');
        }
        Tool_Fnc::ajaxMsg('删除成功',1);
    }
}
?>
